/*
 * ©  2013
 * All Rights Reserved
 */
package dieci.system;

import dieci.database.PersistentObject;
import dieci.utils.Redondeo;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Product implements PersistentObject
{

    private long productId;

    private String productCode;
    
    private String productDescription;
    
    private String productDescriptionLong;
    
    private Set<Price> productPrice = new HashSet<Price>();
    
    private String productSize;
    
    private String productColor;
 
    private double cost;

    private int deleted;
    
    public Product()
    {
    }

    
    public Product(String productCode, String productDescription)
    {
        this.productCode = productCode;
        this.productDescription = productDescription;
    }

        
    public Product(String productCode, String productDescription, String productDescriptionLong, 
            Set<Price> productPrice, String size, String color, double cost, int deleted)
    {
        this.productCode = productCode;
        this.productDescription = productDescription;
        this.productDescriptionLong = productDescriptionLong;
        this.productPrice = productPrice;
        this.productSize = size;
        this.productColor = color;
        this.cost = Redondeo.Redondear(cost,2);
        this.deleted = deleted;
    }
    
    public Product(String productCode, String productDescription, String productDescriptionLong, 
            Price price, String size, String color, double cost, int deleted)
    {
        this.productCode = productCode;
        this.productDescription = productDescription;
        this.productDescriptionLong = productDescriptionLong;
        this.productPrice.add(price);
        this.productSize = size;
        this.productColor = color;
        this.cost = Redondeo.Redondear(cost,2);
        this.deleted = deleted;
    }
    
    public String getProductCode() 
    {
        return productCode;
    }

    public void setProductCode(String productCode) 
    {
        this.productCode = productCode;
    }

    public String getProductDescription() 
    {
        return productDescription;
    }

    public void setProductDescription(String productDescription) 
    {
        this.productDescription = productDescription;
    }

    public String getProductDescriptionLong() 
    {
        return productDescriptionLong;
    }

    public void setProductDescriptionLong(String productDescriptionLong) 
    {
        this.productDescriptionLong = productDescriptionLong;
    }
               
    /**
     * Get the value of productPrice
     *
     * @return the value of productPrice
     */
    public Set<Price> getProductPrice()
    {
        return productPrice;
    }

    public Price getLatestProductPrice()
    {
        Date currentDate = new Date();
        long diff = currentDate.getTime() - 0;
        Price priceToShow = null;
        for (Price price : productPrice) 
        {           
            if((currentDate.getTime() - price.getPriceDate().getTime()) < diff)
            {
                diff = currentDate.getTime() - price.getPriceDate().getTime();
                priceToShow = price;
            }
        }
        return priceToShow;
    }
    
    /**
     * Set the value of productPrice
     *
     * @param productPrice new value of productPrice
     */
    public void setProductPrice(Set<Price> productPrice)
    {
        this.productPrice = productPrice;
    }
    
        
    public String getProductSize()
    {
        return productSize;
    }

    public void setProductSize(String productSize)
    {
        this.productSize = productSize;
    }

    public String getProductColor()
    {
        return productColor;
    }

    public void setProductColor(String productColor)
    {
        this.productColor = productColor;
    }


    /**
     * Get the value of productId
     *
     * @return the value of productId
     */
    public long getProductId()
    {
        return productId;
    }

    /**
     * Set the value of productId
     *
     * @param productId new value of productId
     */
    public void setProductId(long productId)
    {
        this.productId = productId;
    }


    /**
     * Get the value of cost
     *
     * @return the value of cost
     */
    public double getCost()
    {
        return cost;
    }

    /**
     * Set the value of cost
     *
     * @param cost new value of cost
     */
    public void setCost(double cost)
    {
        this.cost = Redondeo.Redondear(cost,2);
    }

    public int getDeleted() {
        return deleted;
    }

    public void setDeleted(int deleted) {
        this.deleted = deleted;
    }


    @Override
    public String toString()
    {
        return this.productCode+" - "+this.productDescription + " - "+ this.productColor;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (!(obj instanceof Product))
            return false;

        return ((Product)obj).getProductId() == this.productId;
    }   
}
